package TUILayer;
import java.util.*;
import ControlLayer.*;
import ModelLayer.*;


/**
 * Write a description of class ItemUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ItemUI
{
    private ItemCtrl itemCtrl;
    private MainMenuUI mmu = new MainMenuUI();
    
    /**
     * Constructor for objects of class ItemUI
     */   
    public ItemUI()
    {
        itemCtrl = new ItemCtrl();
    }
    
    public void startMainMenu()
    {
        boolean _exit = false;
        while (!_exit) //! means while exit not is true (that is: false)
        {
            int _choice = writeMainMenu();
            if(_choice == 1)
            {
                createRentItem();
            }
            else{
                    if(_choice == 2)
                    {
                        getRentItem();
                    }
                    else
                    {
                        if(_choice == 3)
                        {
                            updateRentItem();
                        }
                        else
                        {
                            if(_choice == 4)
                            {
                                removeRentItem();
                            }
                            else
                            {
                                if(_choice == 5)
                                {
                                    listAllRentItems();
                                }
                                else
                                {
                                    if(_choice == 6)
                                    {
                                        createSellItem();
                                    }
                                    else
                                    {
                                        if(_choice == 7)
                                        {
                                            getSellItem();
                                        }
                                        else
                                        {
                                            if(_choice == 8)
                                            {
                                                updateSellItem();
                                            }
                                            else
                                            {
                                                if(_choice == 9)
                                                {
                                                    removeSellItem();
                                                }
                                                else
                                                {
                                                    if(_choice == 10)
                                                    {
                                                        listAllSellItems();
                                                    }
                                                    else
                                                    {
                                                        if(_choice == 11)
                                                        {
                                                            mmu.mainMenuStart();
                                                            _exit = true;
                                                        }
                                                        else
                                                        {
                                            
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    } 
                                }
                            }
                        }
                    }
             }
        }
    }
           
    private int writeMainMenu()
    {       
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** ITEM MENU ***");
            System.out.println("  (1) Create Rent Item");
            System.out.println("  (2) Show Rent Item Information");
            System.out.println("  (3) Update Rent Item");
            System.out.println("  (4) Delete Rent Item");
            System.out.println("  (5) List all Rent Items");
            System.out.println("-----------------------------");
            System.out.println("  (6) Create Sell Item");
            System.out.println("  (7) Show Sell Item Information");
            System.out.println("  (8) Update Sell Item");
            System.out.println("  (9) Delete Sell Item");
            System.out.println(" (10) List all Sell Items");
            System.out.println(" (11) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            
            return choice;
    }
    
    private long inputRentItemId()
    {   
        boolean ok = false;
        long id = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write Rent Item ID:  ");
           try
           {
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
           }
        }//end while
        
        return id;
    }
    
    private long inputSellItemId()
    {   
        boolean ok = false;
        long id = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write Sell Item ID:  ");
           try
           {
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
           }
        }//end while
        
        return id;
    }
    
    private int inputStock()
    {   
        boolean ok = false;
        int stock = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write Sell Item stock:  ");
           try
           {
             stock = keyboard.nextInt();
             if(stock > 0)
               {
                   ok = true;
               }
               else
               {
                   System.out.println("Stock number must be bigger than 0. Please try again");
               }
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
             //keyboard.nextLine();
           }
        }//end while
        
        return stock;
    }
    
    private int inputPrice()
    {   
        boolean ok = false;
        int price = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write Sell Item price:  ");
           try
           {
             price = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
             //keyboard.nextLine();
           }
        }//end while
        
        return price;
    }
    
    private int inputWeeklyRate()
    {   
        boolean ok = false;
        int weeklyRate = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write Rent Item Weekly Rate:  ");
           try
           {
             weeklyRate = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
             //keyboard.nextLine();
           }
        }//end while
        
        return weeklyRate;
    }
    
    private String inputName()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Item name: ");
        String name = keyboard.nextLine();
        
        return name;
    }
    
    private String inputLocation()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type location: ");
        String location = keyboard.nextLine();
        
        return location;
    }
    
    /**
     * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
     * -----------------------Rent Item Methods Part------------------------------
     * <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
     */
    
    
    private void getRentItem()
    {
        System.out.println(" --- Show  Rent Item Information --- ");
        long id = inputRentItemId();
        if(itemCtrl.getRentItem(id) != null)
        {
            RentItem ri = itemCtrl.getRentItem(id);
            System.out.println("ID: " + ri.getId());
            System.out.println("Item name: " + ri.getName());
            System.out.println("Item location: " + ri.getLocation());
            System.out.println("Item category: " + itemCtrl.getRentItemCategoryName());
            if(ri.getStatus() == true)
            {
                System.out.println("Status: Rent Item is currently availiable");
            }
            else
            {
                if(ri.getStatus() == false)
                System.out.println("Status: Rent Item is currently leased");
            }
        }
        else
        {
            System.out.println("Rent Item with this ID does not exist!");
        }
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void createRentItem()
    {
        System.out.println(" --- Create Rent Item ---");
        long id = inputRentItemId();
        if(itemCtrl.getRentItem(id) == null)
        {
            RentItem ri = new RentItem();
            ri.setId(id);
            ri.setName(inputName());
            ri.setLocation(inputLocation());
            ri.setWeeklyRate(inputWeeklyRate());
            ri.setStatusTrue();
            
            itemCtrl.createRentItem(ri);
            System.out.println("Rent Item has been created!");
        }
        
        else
        {
            System.out.println("Rent Item with this ID already exists");
        }
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void updateRentItem()
    {
        System.out.println(" --- Update Rent Item ---");
        long id = inputRentItemId();
        if(itemCtrl.getRentItem(id) != null)
        {
            RentItem ri = new RentItem();
            ri.setId(id);
            ri.setName(inputName());
            ri.setLocation(inputLocation());
            ri.setWeeklyRate(inputWeeklyRate());
            
            itemCtrl.updateRentItem(ri);
            System.out.println("Rent Item has been updated!");
        }
        
        else
        {
            System.out.println("Rent Item with this ID does not exist!");
        }
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void removeRentItem()
    {
        System.out.println(" -- Remove Rent Item --");
        long id = inputRentItemId();
        if(itemCtrl.getRentItem(id) != null)
        {
            itemCtrl.removeRentItem(id);
            System.out.println("Rent Item has been removed!");
        }
        else
        {
            System.out.println("Rent Item with this ID does not exist!");
        }
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void listAllRentItems()
    {
        System.out.println(" --- List Rent Items ---");
        if (itemCtrl.listAllRentItems().size() == 0 )
        {
            System.out.println("There are no Rent Items in the system");
        }
        else
        {
            System.out.println("Results:");
            for (int i=0; i<itemCtrl.listAllRentItems().size(); i++)
            {  
                System.out.println (i+1 +")" + "  ID: " + itemCtrl.listAllRentItems().get(i).getId() + "  Name: " + itemCtrl.listAllRentItems().get(i).getName());
            }
        }
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    /** 
     * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
     * ---------------------Sell Item Methods Part-------------------------------
     * <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
     */
    
    private void getSellItem()
    {
        System.out.println(" --- Show  Sell Item Information --- ");
        long id = inputSellItemId();
        if(itemCtrl.getSellItem(id) != null)
        {
            SellItem si = itemCtrl.getSellItem(id);
            System.out.println("ID: " + si.getId());
            System.out.println("Item name: " + si.getName());
            System.out.println("Item location: " + si.getLocation());
            System.out.println("Item category: " + itemCtrl.getSellItemCategoryName());
            System.out.println("Item quantity: " + si.getStock());
            System.out.println("Item price: " + si.getPrice());
        }
        else
        {
            System.out.println("Sell Item with this ID does not exist!");
        }
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void createSellItem()
    {
        System.out.println(" --- Create Sell Item ---");
        long id = inputSellItemId();
        if(itemCtrl.getSellItem(id) == null)
        {
            SellItem si = new SellItem();
            si.setId(id);
            si.setName(inputName());
            si.setLocation(inputLocation());
            si.setStock(inputStock());
            si.setPrice(inputPrice());
            
            itemCtrl.createSellItem(si);
            System.out.println("Sell Item has been created!");
        }
        
        else
        {
            System.out.println("Sell Item with this ID already exists!");
        }
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void updateSellItem()
    {
        System.out.println(" --- Update Sell Item ---");
        long id = inputSellItemId();
        if(itemCtrl.getSellItem(id) != null)
        {
            SellItem si = new SellItem();
            si.setId(id);
            si.setName(inputName());
            si.setLocation(inputLocation());
            si.setStock(inputStock());
            si.setPrice(inputPrice());
            
            itemCtrl.updateSellItem(si);
            System.out.println("Sell Item has been updated!");
        }
        
        else
        {
            System.out.println("Sell Item with this ID does not exist!");
        }
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void removeSellItem()
    {
        System.out.println(" -- Remove Sell Item --");
        long id = inputSellItemId();
        if(itemCtrl.getSellItem(id) != null)
        {
            itemCtrl.removeSellItem(id);
            System.out.println("Sell Item has been removed!");
        }
        else
        {
            System.out.println("Sell Item with this ID does not exist!");
        }
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void listAllSellItems()
    {
        System.out.println(" --- List Sell Items ---");
        if (itemCtrl.listAllSellItems().size() == 0 )
        {
            System.out.println("There are no Sell Items in the system");
        }
        else
        {
            System.out.println("Results:");
            for (int i=0; i<itemCtrl.listAllSellItems().size(); i++)
            {  
                System.out.println (i+1 +")" + "  ID: " + itemCtrl.listAllSellItems().get(i).getId() + "  Name: " + itemCtrl.listAllSellItems().get(i).getName());
            }
        }
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
}
